package com.ruoyi.web.controller.archive;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PCabinet;
import com.ruoyi.system.domain.vo.HardVersionRequest;
import com.ruoyi.system.service.IPCabinetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 存储柜管理Controller
 *
 * @author ruoyi
 * @date 2024-12-31
 */
@RestController
@RequestMapping("/archive/cabinet")
public class PCabinetController extends BaseController
{
    @Autowired
    private IPCabinetService pCabinetService;


    /**
     * 查询存储柜管理列表
     */
    @PreAuthorize("@ss.hasPermi('archive:cabinet:list')")
    @GetMapping("/list")
    public TableDataInfo list(PCabinet pCabinet)
    {
        startPage();
        List<PCabinet> list = pCabinetService.selectPCabinetList(pCabinet);
        return getDataTable(list);
    }


    /**
     * 查询存储柜管理列表
     */

    @GetMapping("/getList")
    public AjaxResult getList(PCabinet pCabinet)
    {
        return success(pCabinetService.selectPCabinetList(pCabinet));
    }



    /**
     * 导出存储柜管理列表
     */
    @PreAuthorize("@ss.hasPermi('archive:cabinet:export')")
    @Log(title = "存储柜管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PCabinet pCabinet)
    {
        List<PCabinet> list = pCabinetService.selectPCabinetList(pCabinet);
        ExcelUtil<PCabinet> util = new ExcelUtil<PCabinet>(PCabinet.class);
        util.exportExcel(response, list, "存储柜管理数据");
    }

    /**
     * 获取存储柜管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('archive:cabinet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pCabinetService.selectPCabinetById(id));
    }

    /**
     * 新增存储柜管理
     */
    @PreAuthorize("@ss.hasPermi('archive:cabinet:add')")
    @Log(title = "存储柜管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PCabinet pCabinet)
    {
        return toAjax(pCabinetService.insertPCabinet(pCabinet));
    }

    /**
     * 修改存储柜管理
     */
    @PreAuthorize("@ss.hasPermi('archive:cabinet:edit')")
    @Log(title = "存储柜管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PCabinet pCabinet)
    {
        return toAjax(pCabinetService.updatePCabinet(pCabinet));
    }

    /**
     * 删除存储柜管理
     */
    @PreAuthorize("@ss.hasPermi('archive:cabinet:remove')")
    @Log(title = "存储柜管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pCabinetService.deletePCabinetByIds(ids));
    }

    @Log(title = "存储柜管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateHardVersion")
    public AjaxResult updateHardVersion(@RequestBody HardVersionRequest request)
    {
        Map<String, String> failedResult = pCabinetService.updateHardVersion(request);
        StringBuilder sb = new StringBuilder();
        if (!failedResult.isEmpty()) {
            logger.warn("更新完成，失败柜子数量: {}", failedResult.size());
            failedResult.forEach((code, reason) ->
                    sb.append("柜子[").append(code).append("]更新失败，原因: {").append(reason).append("}")
            );
            // 返回包含错误信息的失败响应
            return error("部分柜子更新失败:"+ sb.toString());
        }
        return success();
    }

    @Log(title = "存储柜管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateSingleHardVersion")
    public AjaxResult updateSingleHardVersion(@RequestBody HardVersionRequest request)
    {
        pCabinetService.updateSingleHardVersion(request);
        return success();
    }
}
